package com.hhp.cloud.shop.member.controller;

import java.util.Arrays;
import java.util.Map;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhp.cloud.shop.common.core.enums.BizCodeEnume;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hhp.cloud.shop.member.entity.MemberEntity;
import com.hhp.cloud.shop.member.service.MemberService;

import com.hhp.cloud.shop.common.core.result.R;



/**
 * 会员
 *
 * @author hhp
 * @email 2437579794@qq.com
 * @date 2021-11-24 16:26:44
 */
@RestController
@RequestMapping("/api/v1/member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @ApiOperation(value = "会员分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "username", value = "用户名", paramType = "query", dataType = "String")
    })
    @GetMapping
    public R list(Integer page, Integer limit, String username){
        return R.ok().put("data", memberService.pageMaps(
                new Page<>(page, limit),
                new LambdaQueryWrapper<MemberEntity>()
                        .like(StrUtil.isNotBlank(username), MemberEntity::getUsername, username)));
    }


    /**
     * 信息
     */
    @ApiOperation(value = "会员详情")
    @ApiImplicitParam(name = "id", value = "字典类型id", required = true, paramType = "path", dataType = "Long")
    @GetMapping("/{id}")
    public R info(@PathVariable("id") Long id){
		return R.ok().setData(memberService.getById(id));
    }

    /**
     * 保存
     */
    @ApiOperation(value = "新增会员")
    @PostMapping
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改会员")
    @PutMapping("/{id}")
    public R update(@PathVariable("id") Long id, @RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除会员")
    @DeleteMapping
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @ApiOperation(value = "根据openid获取会员认证信息")
    @ApiImplicitParam(name = "openid", value = "微信身份唯一标识", required = true, paramType = "path", dataType = "String")
    @GetMapping("/openid/{openid}")
    public R getByOpenid(@PathVariable("openid") String openid) {
        Map<String, Object> member = memberService.getMap(
                new LambdaQueryWrapper<MemberEntity>()
                        .eq(MemberEntity::getOpenid, openid)
                        .select(MemberEntity::getId, MemberEntity::getOpenid, MemberEntity::getStatus));
        if (member == null) {
            return R.error(BizCodeEnume.USER_NOT_EXIST);
        }
        return R.ok().setData(member);
    }

}
